package com.shey.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shey.imageloader.MyImageLoader;
import com.shey.qiuckpictest.R;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImageDetailFragment extends Fragment {

	public static String PHOTO_URL = "photoUrl";
	private String mImageUrl;
	private PhotoView mPhotoView;
	private PhotoViewAttacher mAttacher;

	public static ImageDetailFragment newInstance(String imageUrl) {
		final ImageDetailFragment fragment = new ImageDetailFragment();

		final Bundle args = new Bundle();
		args.putString(PHOTO_URL, imageUrl);
		fragment.setArguments(args);

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageUrl = getArguments() != null ? getArguments().getString(PHOTO_URL) : null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View viewLayout = inflater.inflate(R.layout.item_fragment_viewpager, container, false);
		mPhotoView = (PhotoView) viewLayout.findViewById(R.id.item_fragment_viewpager);

		mAttacher = new PhotoViewAttacher(mPhotoView);
		mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
			@Override
			public void onPhotoTap(View arg0, float arg1, float arg2) {
				getActivity().finish();
			}
		});

		return viewLayout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MyImageLoader.getInstance().displayImageOriginal(mPhotoView, mImageUrl, new MyImageLoader.LoadeImageCallBack() {
			@Override
			public void onLoadeImage(Bitmap bitmap, ImageView imageView, String uri) {
				if (imageView.getTag() != null && imageView.getTag().equals(uri)) {
					imageView.setImageBitmap(bitmap);
					mAttacher.update();
				}
			}
		});
	}
}